package com.zb;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

@Configuration
@EnableConfigurationProperties(TenantConfigurationProperties.class)
@MapperScan(basePackages = "com.zb.mapper")
public class TenantAutoConfiguration {

    @Bean
    public TenantDynamicDataSource tenantDynamicDataSource(TenantConfigurationProperties properties) {
        TenantDynamicDataSource dataSource = new TenantDynamicDataSource();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("default", properties.getDefaultProfileDataSource());

        dataSource.setTargetDataSources(map);
        dataSource.setDefaultTargetDataSource(properties.getDefaultProfileDataSource());
        return dataSource;
    }

    @Bean(name = "tenantSqlSessionFactory")
    public MybatisSqlSessionFactoryBean sqlSessionFactory(
            @Qualifier("tenantDynamicDataSource") DataSource dataSource)
            throws IOException {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        // 绑定动态数据源
        sessionFactory.setDataSource(dataSource);
        // 配置核心模块的 Mapper 映射文件路径
        return sessionFactory;
    }

    @Bean(name = "tenantSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(
            @Qualifier("tenantSqlSessionFactory") MybatisSqlSessionFactoryBean sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(Objects.requireNonNull(sqlSessionFactory.getObject()));
    }

    /**
     * 配置分页插件  也可以选择自己手搓
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        // 配置驼峰和标准化日志输出
        return configuration -> {
            configuration.setMapUnderscoreToCamelCase(true);
            configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        };
    }

    /**
     * 注入事务管理器
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
